/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package org.xxforest.game.core.scenes.scene2d.ui;

import org.xxforest.game.core.scenes.scene2d.XXNode;
import org.xxforest.game.core.utils.XXPoint;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class XXImage extends XXNode {

	protected TextureRegion textureRegion;     

	protected XXImage() {}
	
	public XXImage(TextureRegion textureRegion) {
		this.setScale(1, 1);
		this.setTextureRegion(textureRegion);
		this.unTouchAble() ;
	}

	@Override
	public void draw(SpriteBatch batch , float delta) {
		
		Color color = getColor();
		float parentAlpha = getDrawAlpha() ;
		batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);

		float x = getDrawX();
		float y = getDrawY();
		float drawWith = getDrawWidth() ;
		float drawHeight = getDrawHeight() ;
		float originX = getDrawAnchorPointX();
		float originY = getDrawAnchorPointY() ;
		float rotation = getDrawRotation();
		
		if(rotation != 0 ){
			batch.draw(textureRegion, x - originX, y - originY , originX ,originY, drawWith, drawHeight, 1, 1,180 + rotation);
		}else{
			batch.draw(textureRegion, x - originX, y - originY, drawWith, drawHeight) ;
		}
		
	}

	public float getImageWidth () {
		return textureRegion.getRegionWidth();
	}

	public float getImageHeight () {
		return textureRegion.getRegionHeight();
	}

	public TextureRegion getTextureRegion() {
		return textureRegion;
	}

	public void setTextureRegion(TextureRegion textureRegion) {
		this.textureRegion = textureRegion;
		this.textureRegion = textureRegion;
		this.setWidth(textureRegion.getRegionWidth());
		this.setHeight(textureRegion.getRegionHeight()) ;
		this.setAnchorPoint(XXPoint.create(getWidth() / 2, getHeight() /2));
	}

	public static XXImage create(String path) {
		return create(new Texture(Gdx.files.internal(path)));
	}
	
	public static XXImage create(Texture texture) {
		return new XXImage(new TextureRegion(texture));
	}
	
	public static XXImage create(TextureRegion textureRegion) {
		return new XXImage(textureRegion);
	}

	@Override
	public void onCreate() {
		
	}

	
}
